import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Article } from './article';
import {Articles} from './mock-article';

@Injectable()
export class ArticleService {

  private headers = new Headers({'Content-Type': 'application/json'});

  /**
   * 声明文章列表api接口
   * **/
  private articlesUrl = 'http://localhost/api/article';

  constructor(private http: Http) {}


  /**
   * 获取文章列表
   * **/
  getArticles(): Promise<Article[]> {
    console.log('api url:' + this.articlesUrl);
    this.http.get(this.articlesUrl).toPromise().then(response => console.log(response.json())).catch(this.handleError);
    this.http.get(this.articlesUrl).toPromise().then(response => console.log('data:' + response.json().data)).catch(this.handleError);
    return this.http.get(this.articlesUrl).toPromise().then(response => response.json() as Article[]).catch(this.handleError);
    // return Promise.resolve(Articles);
  }

  private handleError(error: any): Promise<any> {
    console.log('An error occurred:' + error)
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}
